package com.dyb.recuitment.controller;


import com.dyb.recuitment.common.Result;
import com.dyb.recuitment.entity.Company;
import com.dyb.recuitment.entity.UserLog;
import com.dyb.recuitment.service.ICompanyService;
import com.dyb.recuitment.service.IUserLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 企业表 前端控制器
 * </p>
 *
 * @author 邓宇斌
 * @since 2022-02-23
 */
@RestController
@Api(tags = "公司管理(Company)")
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private ICompanyService companyService;

    @Autowired
    private IUserLogService userLogService;

    @ApiOperation(value = "添加公司", notes = "通过公司统一信用代码、公司名称、申请人" +
            "{\"companyNo\":\"\",\"companyName\":\"\",\"createBy\":1}")
    @ApiImplicitParam(name = "company", value = "Company类")
    @PostMapping("/add")
    public Object add(@RequestBody Company company) {
        Object data = null;
        try {
            data = companyService.add(company);
            userLogService.add(new UserLog(company.getCreateBy(), "添加公司"));
            if (data == null || (int) data == 0) {
                return Result.failure(data, "添加失败!");
            }
            return Result.success(data, "添加成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "公司详情", notes = "通过ID查询公司详情 {\"id\":1}")
    @ApiImplicitParam(name = "company", value = "Company类")
    @PostMapping("/selectById")
    public Object selectById(@RequestBody Company company) {
        Object data = null;
        try {
            data = companyService.getById(company);
            if (data == null) {
                return Result.warning(data, "公司为空!");
            }
            return Result.success(data, "查询成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "修改公司", notes = "修改公司 {\"\":\"\",\"id\":1}")
    @ApiImplicitParam(name = "company", value = "Company类")
    @PostMapping("/update")
    public Object update(@RequestBody Company company) {
        Object data = null;
        try {
            data = companyService.update(company);
            if (data == null || data == (Integer) 0) {
                return Result.failure(data, "修改失败!");
            }
            return Result.success(data, "修改成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "审核公司", notes = "通过ID审核公司 {\"auditStatus\":1,\"id\":1}")
    @ApiImplicitParam(name = "company", value = "Company类")
    @PostMapping("/audit")
    public Object audit(@RequestBody Company company) {
        Object data = null;
        try {
            data = companyService.audit(company);
            if (data == null || data == (Integer) 0) {
                return Result.failure(data, "审核失败!");
            }
            return Result.success(data, "审核成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "删除公司", notes = "通过ID删除公司" +
            "{\"id\":1}")
    @ApiImplicitParam(name = "company", value = "Company类")
    @PostMapping("/delete")
    public Object delete(@RequestBody Company company) {
        Object data = null;
        try {
            data = companyService.delete(company);
//            userLogService.add(new UserLog(resume.getUserId(), "删除公司"));
            if (data == null || data == (Integer) 0) {
                return Result.failure(data, "删除失败!");
            }
            return Result.success(data, "删除成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "按条件查询公司", notes = "按条件查询公司 {\"companyNo\":\"\"}")
    @ApiImplicitParam(name = "company", value = "Company类")
    @PostMapping("/selectByCompany")
    public Object selectByCompany(@RequestBody Company company) {
        Object data = null;
        try {
            data = companyService.selectByCompany(company);
            if (data == null || ((List) data).size() <= 0) {
                return Result.warning(data, "暂无公司!");
            }
            return Result.success(data, "查询成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "查询所有公司", notes = "查询所有公司")
    @GetMapping("/selectAllCompany")
    public Object selectAllCompany() {
        Object data = null;
        try {
            data = companyService.selectAllCompany();
            if (data == null || ((List) data).size() <= 0) {
                return Result.warning(data, "暂无公司!");
            }
            return Result.success(data, "查询成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

}
